$(document).ready(function() {
	$('.hideable').hide(0);
	$('#pwd').attr('disabled', 'disabled');
	$('#pwd_verif').attr('disabled', 'disabled');
	$('#groupe').attr('disabled', 'disabled');
	$('#admingroup').attr('disabled', 'disabled');
	$('#switch-no-group').iphoneSwitch("on",
			function() {
		$('#noGroup').attr('checked', true);
		$('#pwd').val('');
		$('#pwd_verif').val('');
		pwdOk($('#pwd').val());
		verifPwdOk($('#pwd').val());
		$('.hideable').hide(300);
		$('#pwd').attr('disabled', 'disabled');
		$('#pwd_verif').attr('disabled', 'disabled');
		$('#groupe').attr('disabled', 'disabled');
		$('#admingroup').attr('disabled', 'disabled');
	},
	function() {
		$('#noGroup').attr('checked', false);
		$('.hideable').show(300);
		$('#pwd').removeAttr('disabled');
		$('#pwd_verif').removeAttr('disabled');
		$('#groupe').removeAttr('disabled');
		$('#admingroup').removeAttr('disabled');
	},
	{
		switch_on_container_path: '/css/jqueryui/iphone-switch/iphone_switch_container_on.png',
		switch_off_container_path: '/css/jqueryui/iphone-switch/iphone_switch_container_off.png',
		switch_path: '/css/jqueryui/iphone-switch/iphone_switch.png'
	});

	$('#switch-admin').iphoneSwitch("off",
			function() {
		$('#admingroup').attr('checked', true);
	},
	function() {
		$('#admingroup').attr('checked', false);
	},
	{
		switch_on_container_path: '/css/jqueryui/iphone-switch/iphone_switch_container_on.png',
		switch_off_container_path: '/css/jqueryui/iphone-switch/iphone_switch_container_off.png',
		switch_path: '/css/jqueryui/iphone-switch/iphone_switch.png'
	});
});

function loginExists(arg) {
	if(arg == "") {
		$('#exist').html("");
	} else {
		$.ajax({
			url: "loginexist",
			type: "post",
			data: {login:[arg]},
			dataType: "json",
			success: function(data) {
				if(data.exist) {
					$('#exist').html("<img style='width:16 px; height: 16px' src='/css/main/images/icons/cross.png'/>");
				} else {
					$('#exist').html("<img style='width:16 px; height: 16px' src='/css/main/images/icons/tick.png'/>");
				}
			},
			error: function(e,xhr) {
				alert("Une erreur inconnue a eu lieu.");
			}
		});
	}
}

function pwdOk(data) {
	if(data.length == 0) {
		$('#pwd-ok').html("");
	} else if(data.length > 5) {
		$('#pwd-ok').html("<img style='width:16 px; height: 16px' src='/css/main/images/icons/tick.png'/>");
	} else {
		$('#pwd-ok').html("<img style='width:16 px; height: 16px' src='/css/main/images/icons/cross.png'/>");
	}
}

function verifPwdOk(data) {
	if(data.length == 0) {
		$('#pwd-verif-ok').html("");
	} else if(data == $('#pwd').val()) {
		$('#pwd-verif-ok').html("<img style='width:16 px; height: 16px' src='/css/main/images/icons/tick.png'/>");
	} else {
		$('#pwd-verif-ok').html("<img style='width:16 px; height: 16px' src='/css/main/images/icons/cross.png'/>");
	}
}